<?php

/*
 * 获取日志文件最后 n 行
 *
 * */


## 默认行数
$line = 100;
$max_line = 1000;
try {
    if (isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO'])){
        // index.php/2/3
        $path_info = explode('/', $_SERVER['PATH_INFO']);
        if(!empty($path_info[1])){
            $line = $path_info[1];
        }
        if(!is_int($line)){
            echo '参数错误，请输入整形数字';
            die;
        }
        if($line > $max_line ){
            echo '获取文件行数 '. $line . ' 过大,最多获取 ' . $max_line . ' 行';
            die;
        }
    }

    ## 日志位置
    $file_path = '/var/log/app.log';
    $cli = 'tail -'. $line . " " . $file_path;
    exec($cli, $rows);
    foreach ($rows as $row){
        echo '<p>' .  $row . '</p>';
    }

} catch (Exception $e){
    echo $e->getMessage();
    die;
}   